package com.uyaer.magic.layer
{
	import com.uyaer.magic.common.Common;
	import com.uyaer.magic.events.EventManager;
	import com.uyaer.magic.events.EventType;
	import com.uyaer.magic.events.MyEvent;
	import com.uyaer.magic.nape.BodyManager;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyType;
	import nape.phys.Material;
	import nape.shape.Polygon;
	
	public class CanvasLayer extends Sprite
	{
		private var g:Graphics;
		private var isDown:Boolean;
		private var pos:Vector.<Number>;
		private var oldPos:Point = new Point();
		private var nowPos:Point = new Point();
		private var mTotalDistance:Number = 0;
		private var mDistance:Number = 0;
		public function CanvasLayer()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded(e:Event = null):void{
			this.g = graphics;
			stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseEvent);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseEvent);
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseEvent);
			EventManager.addEvent(EventType.GAME_RESET,onGameResetHandler);
		}
		
		private function onGameResetHandler(e:MyEvent):void{
			this.clear();			
		}
		
		private function onMouseEvent(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					isDown = true;
					pos = new Vector.<Number>();
					g.lineStyle(10,0xffff22);
					g.moveTo(mouseX,mouseY);
					pos.push(mouseX,mouseY);
					oldPos.setTo(mouseX,mouseY);
					break;
				case MouseEvent.MOUSE_MOVE:
					nowPos.setTo(mouseX,mouseY);
					mDistance = Point.distance(oldPos,nowPos);
					if(isDown && mDistance>=10){
						mTotalDistance += mDistance;
						oldPos.setTo(mouseX,mouseY);
						g.lineTo(mouseX,mouseY);
						pos.push(mouseX,mouseY);
					}
					break;
				case MouseEvent.MOUSE_UP:
					isDown = false;
					createBodys();
					break;
			}	
		}
		
		private function createBodys():void{
			if(pos.length>=4){
				var x1:Number;
				var x2:Number;
				var y1:Number;
				var y2:Number;
				var dx:Number;
				var dy:Number;
				var rad:Number;
				var body:Body = new Body(BodyType.STATIC);
				var shape:Polygon;
				var v2:Vec2 = new Vec2();
				var material:Material = new Material(0,0.00001,0.00001,0.35);
				for (var i:int = 0; i < pos.length-2; i+=2){
					x1 = pos[i];
					y1 = pos[i+1];
					x2 = pos[i+2];
					y2 = pos[i+3];
					dx = x2-x1;
					dy = y2-y1;
					rad = Math.atan2(dy,dx);
					oldPos.setTo(x1,y1);
					nowPos.setTo(x2,y2);
					shape = new Polygon(Polygon.box(Point.distance(oldPos,nowPos),10));
					shape.material = material;
					v2.setxy((x2+x1)*0.5,(y2+y1)*0.5);
					shape.translate(v2);
					shape.rotate(rad);
					shape.localCOM = v2;
					body.shapes.add(shape);
				}
				body.space = Common.space;
				
				BodyManager.i.ballStartMove(new Vec2(500,0));
			}
		}
		
		public function clear():void{
			mTotalDistance = 0;
			g.clear();
		}
	}
}